package org.za.communication.core.handler;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.handler.chain.ChainedIoHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.za.communication.message.RegisterMessage;

import android.R.integer;
import android.util.Log;

public class SessionIdleHandler extends ChainedIoHandler {

	private final static Logger log = LoggerFactory
			.getLogger(SessionIdleHandler.class);

	private int count = 0;
	public static String flag = "";
	public static byte[] key = new byte[4]; // 密钥的全局变量
	public static boolean go = false;
	// public static String md5s = "";	

	public static long message_id;
	public static String[] md5s = new String[]{"","",""};
	public static String returnlist ="";
	
	public static int zhilu;
	public static int textreturn;
	
	public static IoSession ioSession;

	public void sessionCreated(IoSession session) {
		log.info("session created");
	}

	public void sessionOpened(IoSession session) throws Exception {
		count++;
		log.info("client " + count + " conneccted : "
				+ session.getRemoteAddress().toString());
	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		try {
			this.ioSession = session;
			log.info("Server Say:" + message.toString());
			IoBuffer buf = (IoBuffer) message;

			int msg = buf.getUnsignedShort();

			if (msg == 0x99) {

				key[0] = buf.get();
				key[1] = buf.get();
				key[2] = buf.get();
				key[3] = buf.get();

				// buf.get(key);

				go = true;
			}
			if (msg == 0x01) {
				int f = buf.get();
				System.out.println("Server Say:msg " + f + "<-f");
				Log.i("mylog", f + "<-f");
				if (f == 0) {

					flag = "ok";
				}
				if (f == 1) {
					flag = "no";
				}
				if (f == 2) {
					flag = "pass";
				}
				if (f == 3) {
					flag = "other";
				}
			}
			if (msg == 0x02) {
				System.out.println("开始登录返回数据");
				System.out.println("1 buf.position(): " + buf.position());
				int f = buf.get();
				System.out.println("2 buf.position(): " + buf.position());
				System.out.println("Server Say:login " + f + "<-f");
				if (f == 0) {
					flag = "ok";
					long sessionid = buf.getUnsignedInt();

					System.out.println("sessionid" + sessionid);

					int index1 = buf.indexOf((byte) 0x00) - buf.position() ;

					System.out.println("index1: " + index1);
					if (index1 == 0) {
						buf.get();
					} else if (index1 > 0) {
						md5s[0] = buf.getString(index1, Charset.forName("utf-8")
								.newDecoder());
						buf.get();
					}
					
					int index2 = buf.indexOf((byte) 0x00) - buf.position() ;

					System.out.println("index2: " + index2);
					if (index2 == 0) {
						buf.get();
					} else if (index2 > 0) {
						md5s[1] = buf.getString(index2, Charset.forName("utf-8")
								.newDecoder());
						buf.get();
					}
					
					int index3 = buf.indexOf((byte) 0x00) - buf.position() ;

					System.out.println("index3: " + index3);
					if (index3 == 0) {
						buf.get();
					} else if (index3 > 0) {
						md5s[2] = buf.getString(index3, Charset.forName("utf-8")
								.newDecoder());
						buf.get();
					}

				}
				if (f == 1) {
					flag = "no";// 没有注册
				}
				if (f == 2) {
					flag = "pass";// 密码错误
				}
				if (f == 3) {
					flag = "other";
				}
			}
			//获取好友列表
			if (msg==0x04) {
				buf.get(key);//获取key
				int type = buf.get();
				int length = (int)buf.getUnsignedInt();
				String content = buf.getString(length, Charset.forName("utf-8").newDecoder());
				returnlist = content;
			}
			if (msg==0x05) {
				
				zhilu = buf.get();
				message_id = buf.getUnsignedInt();
				go = true;
				
			}
			if (msg==0x06) {
				textreturn = buf.get();
				message_id = buf.getUnsignedInt();
				go = true;
			}

		} catch (Exception ee) {
			log.error("", ee);
		}
	}

	@Override
	public void messageSent(IoSession session, Object message) {
		log.info("Client Say:" + message.toString());
	}

	@Override
	public void sessionClosed(IoSession session) {
		log.info("one Clinet Disconnect ! "
				+ session.getRemoteAddress().toString());
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) {
		log.info("one Clinet sessionIdle ! ");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) {
		log.error("" + cause);
	}

}
